package com.tanhua.exception;

import com.tanhua.vo.ErrorResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @ControllerAdvice 用于集中处理控制器的异常，可以定义一些通用的异常处理逻辑
 * 加上这个额注解的类，就是全局异常处理类
 */
@ControllerAdvice
public class ExceptionAdvice {

    /**
     * 处理业务异常
     */
    @ExceptionHandler(MyException.class)
    public ResponseEntity handlerMyException(MyException me){
        me.printStackTrace();
        ErrorResult errorResult = me.getErrorResult();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 未知异常
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity handlerException(Exception e){
        e.printStackTrace();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.error());
    }


}
